package org.jks.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.model.Msg;
import org.jks.model.MsgTask;
import org.jks.model.User;
import org.jks.request.model.Ids;
import org.jks.request.model.UIAddMsg;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.MsgTaskService;
import org.jks.service.UserService;
import org.jks.util.DateUtil;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/sms_task")
public class MsgTaskController {
	@Autowired
	MsgTaskService msgTaskService;
	@Autowired
	UserService userService;
	
	@RequestMapping("add")
	public void add(UIAddMsg uiAddMsg, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			MsgTask msgTask = new MsgTask();
			msgTask.setNotifyContent(uiAddMsg.getNotifyContent());
			msgTask.setNotifyTime(DateUtil.toDate(uiAddMsg.getNotifyTime(), "yyyy-MM-dd HH:mm:ss"));
			msgTask.setNotifyTimes(uiAddMsg.getNotifyTimes());
			if(uiAddMsg.getUserIds() != null){
				List<Msg> msgs = new ArrayList<Msg>();
				for(long id : uiAddMsg.getUserIds()){
					User user = userService.findById(id);
					user.setRoles(null);
					Msg msg = new Msg();
					msg.setMsgTask(msgTask);
					msg.setUser(user);
					msgs.add(msg);
				}
				msgTask.setMsgs(msgs);
			}
			msgTaskService.save(msgTask);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			for(long id: ids.getIds()){
				MsgTask msgtask = msgTaskService.findById(id);
				if(msgtask != null){
					msgTaskService.delete(msgtask);
				}
			}
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			msgTaskService.delete(id);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, Map<String, Object> model, HttpServletResponse response){
		try{
			MsgTask msgTask = msgTaskService.findById(id);
			model.put("msgtask", JsonUtil.Object2Json(msgTask));
		}
		catch(Exception e){
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("sysMgr/msg_task_detail", model);
	}
	
	
	@RequestMapping("/update")
	public void update(UIAddMsg uiAddMsg, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			MsgTask msgTask = msgTaskService.findById(uiAddMsg.getId());
			msgTask.setId(uiAddMsg.getId());
			msgTask.setNotifyContent(uiAddMsg.getNotifyContent());
			msgTask.setNotifyTime(DateUtil.toDate(uiAddMsg.getNotifyTime(), "yyyy-MM-dd HH:mm:ss"));
			msgTask.setNotifyTimes(uiAddMsg.getNotifyTimes());
			msgTask.getMsgs().clear();
			msgTaskService.update(msgTask);
			if(uiAddMsg.getUserIds() != null){
				List<Msg> msgs = new ArrayList<Msg>();
				for(long id : uiAddMsg.getUserIds()){
					User user = new User();
					user.setId(id);
					Msg msg = new Msg();
					msg.setMsgTask(msgTask);
					msg.setUser(user);
					msgs.add(msg);
				}
				msgTask.setMsgs(msgs);
			}
			msgTaskService.update(msgTask);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = msgTaskService.count();
			List<MsgTask> msgTasks = msgTaskService.list(page, pagesize);
			if(msgTasks != null && msgTasks.size() > 0){
				for(MsgTask msgTask : msgTasks){
					List<Msg> msgs = msgTask.getMsgs();
					if(msgs != null && msgs.size() > 0){
						for(Msg msg: msgs){
							msg.getUser().setRoles(null);
							msg.setMsgTask(null);
						}
					}
				}
			}
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(msgTasks);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
}
